define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {
    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'pay/pay/index',
                    fill_url: 'pay/pay/fill',
                    del_url: 'pay/pay/del',
                    table: 'pay', // 表名
                },
            });

            // 获取到table标签的id
            var table = $("#table")

            // 初始化表格
            table.bootstrapTable({
                // 当访问列表发起ajax请求数据
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                // 主键
                pk: 'id',
                // 设置默认排序
                sortName: 'createtime',
                sortOrder: 'desc',
                fixedColumns: true,
                fixedNumber: 1,
                fixedRightNumber: 1,
                columns: [
                    [
                        { checkbox: true },
                        { field: 'id', title: __('Id'), sortable: true, operate: false },
                        { field: 'code', title: __('Pcode'), operate: 'LIKE' },
                        { field: 'name', title: __('Pname'), operate: 'LIKE' },
                        { field: 'type', title: __('Ptype'), 
                            searchList: { 
                                "wx": __('微信支付'), 
                                "zfb": __('支付宝支付') 
                            }, 
                            operate: 'FIND_IN_SET', 
                            formatter: Table.api.formatter.normal, 
                            custom: {"zfb": "success" }
                        },
                        { field: 'total', title: __('Ptotal'), operate: 'RANGE' },
                        { field: 'price', title: __('Pprice'), operate: 'RANGE' },
                        { field: 'remarks', title: __('Premarks'), operate: 'LIKE' },
                        { field: 'jump', title: __('Pjump'), operate: 'LIKE', formatter: Table.api.formatter.url },
                        { field: 'notice', title: __('Pnotice'), operate: 'LIKE', formatter: Table.api.formatter.url },
                        { field: 'status', title: __('Pstatus'), 
                            searchList: { 
                                "0": __('待支付'), 
                                "1": __('已支付'), 
                                "2": __("已关闭") 
                            }, 
                            operate: 'FIND_IN_SET', 
                            formatter: Table.api.formatter.flag, 
                            custom: {"0": "warning", "1": "success", "2": "danger" } 
                        },
                        { field: 'wxcode', title: __('Pwxcode'), formatter: Table.api.formatter.image, operate: false },
                        { field: 'zfbcode', title: __('Pzfbcode'), formatter: Table.api.formatter.image, operate: false },
                        { field: 'createtime', title: __('Pcreatetime'), operate: 'RANGE', addclass: 'datetimerange', formatter: Table.api.formatter.datetime },
                        { field: 'paytime', title: __('Ppaytime'), operate: 'RANGE', addclass: 'datetimerange', formatter: Table.api.formatter.datetime },
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate,
                            buttons: [
                                {
                                    name: 'fill',
                                    title: function (item) {
                                        return __('补单');
                                    },
                                    classname: 'btn btn-xs btn-success btn-ajax',
                                    icon: 'fa fa-leaf',
                                    url: $.fn.bootstrapTable.defaults.extend.fill_url,
                                    confirm: '确认要补单吗？',
                                    success: function (data, ret) {
                                        // 刷新当前页面
                                        // location.reload();
                                        // 刷新当前表格
                                        table.bootstrapTable('refresh');
                                    },
                                    error: function (data, ret) {
                                        console.log(data, ret);
                                        Layer.alert(ret.msg);
                                        return false;
                                    },
                                    // 订单支付状态为已支付时，隐藏补单按钮
                                    hidden: function (item) {
                                        return item.status == 1;
                                    }
                                },
                            ]
                        }
                    ]
                ],
            })

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        // 添加
        add: function () {
            // 为表单绑定事件
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        del: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    }

    return Controller
})